package com.gobang.controller;

import com.gobang.domain.dto.CancelMatchRequestDTO;
import com.gobang.domain.dto.MatchRequestDTO;
import com.gobang.domain.vo.MatchResponseVO;
import com.gobang.domain.vo.Result;
import com.gobang.service.MatchService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 匹配系统HTTP接口控制器
 * 处理匹配相关的HTTP请求，提供RESTful API
 *
 * @author gobang-team
 * @since 1.0.0
 */
@RestController
@RequestMapping("/api/match")
@RequiredArgsConstructor
@Validated
@Slf4j
public class MatchController {

    private final MatchService matchService;

    /**
     * 加入匹配队列
     * 用户请求加入在线匹配队列，系统基于积分进行就近匹配
     *
     * @param request 匹配请求DTO
     * @return 匹配响应结果
     */
    @PostMapping("/join")
    public Result<MatchResponseVO> joinMatch(@Valid @RequestBody MatchRequestDTO request) {
        log.info("用户{}请求加入匹配队列", request.getUserId());
        
        try {
            MatchResponseVO response = matchService.joinMatchQueue(request.getUserId());
            log.info("用户{}成功加入匹配队列，队列位置：{}，预估等待时间：{}秒", 
                request.getUserId(), response.getQueuePosition(), response.getEstimatedWaitTime());
            
            return Result.success(response, "成功加入匹配队列");
            
        } catch (Exception e) {
            log.error("用户{}加入匹配队列失败", request.getUserId(), e);
            throw e;
        }
    }

    /**
     * 取消匹配
     * 用户取消匹配，退出匹配队列
     *
     * @param request 取消匹配请求DTO
     * @return 取消匹配响应结果
     */
    @PostMapping("/cancel")
    public Result<Boolean> cancelMatch(@Valid @RequestBody CancelMatchRequestDTO request) {
        log.info("用户{}请求取消匹配，原因：{}", request.getUserId(), request.getCancelReason());
        
        try {
            boolean success = matchService.cancelMatch(request.getUserId());
            log.info("用户{}取消匹配结果：{}", request.getUserId(), success);
            
            return Result.success(success, "成功取消匹配");
            
        } catch (Exception e) {
            log.error("用户{}取消匹配失败", request.getUserId(), e);
            throw e;
        }
    }

    /**
     * 获取匹配系统状态
     * 管理接口，用于监控匹配系统运行状态
     *
     * @return 匹配系统状态信息
     */
    @GetMapping("/status")
    public Result<MatchService.MatchSystemStatus> getMatchSystemStatus() {
        log.debug("获取匹配系统状态");
        
        try {
            MatchService.MatchSystemStatus status = matchService.getMatchSystemStatus();
            return Result.success(status, "获取匹配系统状态成功");
            
        } catch (Exception e) {
            log.error("获取匹配系统状态失败", e);
            throw e;
        }
    }

    /**
     * 手动触发匹配
     * 管理接口，用于手动触发匹配算法执行
     *
     * @return 触发结果
     */
    @PostMapping("/trigger")
    public Result<String> triggerMatch() {
        log.info("手动触发匹配算法");
        
        try {
            matchService.triggerMatch();
            return Result.success("匹配算法触发成功");
            
        } catch (Exception e) {
            log.error("手动触发匹配失败", e);
            throw e;
        }
    }
} 